package com.ruitu.mall_98_1.search;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyRefreshBaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.dao.api.APIRetrofit;
import com.ruitu.mall_98_1.product.activity.ShopDetailActivity;
import com.ruitu.mall_98_1.product.adapter.ShopListAdapter;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.Company;
import com.ruitu.router_module.bean.product.SearchCondition;
import com.ruitu.router_module.utils.RcvUtils;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ShopListBySearchActivity extends MyRefreshBaseActivity {
    private TextView empty_view;//空白布局

    private int currPage = 1;
    private int totalPage = 1;
    private int totalNum;
    private SearchCondition condition;//传过来的搜索条件

    private List<Company> companyList = new ArrayList<>();//企业(店铺)列表
    private ShopListAdapter adapter;

    protected int getLayoutRes() {
        return R.layout.activity_shop_list_by_search;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        empty_view = findViewById(R.id.empty_view);
        setHeadTitle("企业列表");
        Bundle bundle = getIntent().getExtras();
        condition = (SearchCondition) bundle.getSerializable("condition");
        reqFirst();
        showProgress();
    }

    //执行搜索商品
    private void reqSearchProResult() {
        APIRetrofit.getDefault().reqSearchShopResult(condition.getProvinceId(),
                condition.getCityId(), condition.getCountyId(), condition.getClassA(),
                condition.getClassB(), condition.getClassC(), condition.getKeys(),
                condition.getCityName(), "1", condition.getGoodSaleModel(),
                currPage, 20)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<PageModel<Company>>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        e.printStackTrace();
                        showEmptyView(true);
                    }

                    @Override
                    public void onNext(BaseResult<PageModel<Company>> pageResult) {
                        hideProgress();
                        if (pageResult.isSuccess()) {
                            currPage = pageResult.getData().getCurrentPage();
                            totalPage = pageResult.getData().getTotalPage();
                            totalNum = pageResult.getData().getTotal();
                            List<Company> newData = pageResult.getData().getDataList();
                            companyList.addAll(newData);
                            adapter.setDataList(companyList);
                            if (isDataEmpty(companyList)) {
                                showEmptyView(true);
                            } else {
                                showEmptyView(false);
                            }
                        } else {
                            ToastUtils.showShort(pageResult.getMsg());
                            showEmptyView(true);
                        }
                    }
                });
    }

    private void showEmptyView(boolean isShow) {
        if (isShow) {
            empty_view.setVisibility(View.VISIBLE);
        } else {
            empty_view.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("companyId", companyList.get(position).getId());
        startActivity(ShopDetailActivity.class, bundle);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        companyList.clear();
        reqSearchProResult();

        RcvUtils.refreshCompleteDelay(lRecyclerView, null);
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqSearchProResult();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new ShopListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.l_rcv_shop);
    }

    protected void reReq() {
    }

    protected ProgressLayout findProgressLayout() {
        return null;
    }

    protected void reqData() {
    }
}
